<?php
class Mchapter extends CI_Model{
    
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }
    
	public function getChapterAll(){
        $this->db->select(' id, book_id, no, volume_no, name, uname, open, page_title, page_keyword, page_description,
        					description, mob_description, status, prev_no, next_no, create_date, update_date, view
                         ');
        $this->db->from('chapter');
        $query = $this->db->get();
        return $query->result_array();
	}
	
    public function getChapter(){
        $this->db->select(' chapter.id as chapter_id, chapter.no as chapter_no, chapter.volume_no as chapter_volume_no,
                            chapter.name as chapter_name, chapter.uname as chapter_uname, chapter.book_id as chapter_book_id,
                            chapter.open as chapter_open, chapter.description chapter_description,
                            book.name as book_name, volume.no as volume_no, volume.name as volume_name
                         ');
        $this->db->from('chapter');
        $this->db->join('book'        , 'book.id = chapter.book_id', 'inner');
        $this->db->join('volume'        , 'volume.no = chapter.volume_no', 'left');
        $this->db->order_by('chapter.id', 'desc');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getChapterByLimit($number, $offset){
        $this->db->select(' chapter.id as chapter_id, chapter.no as chapter_no, chapter.volume_no as chapter_volume_no,
                            chapter.name as chapter_name, chapter.uname as chapter_uname, chapter.book_id as chapter_book_id,
                            chapter.open as chapter_open, next_no, prev_no,
                            book.name as book_name, volume.no as volume_no, volume.name as volume_name
                         ');
        $this->db->from('chapter');
        $this->db->join('book'        , 'book.id = chapter.book_id', 'inner');
        $this->db->join('volume'        , 'volume.no = chapter.volume_no AND volume.book_id = chapter.book_id', 'left');
        $this->db->order_by('chapter.id', 'desc'); 
        $this->db->limit($number, $offset);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function insertChapter($param){
        $data = array(
            'id' => NULL,
            'book_id' => $param['book_id'],
            'no' => $param['no'],
            'volume_no' => $param['volume_no'],
            'name' => $param['name'],
            'uname' => $param['uname'],
            'open' => $param['open'],
            'page_title' => $param['page_title'],
            'page_keyword' => $param['page_keyword'],
            'page_description' => $param['page_description'],
            'description' => $param['description'],
            'mob_description' => $param['mob_description'],
            'status' => 1,
        	'prev_no' => $param['prev_no'],
        	'next_no' => $param['next_no']
        );
        $this->db->insert('chapter', $data);
        return $this->db->insert_id();
    }

    public function updateChapter($param){
        $data = array(
            'book_id' => $param['book_id'],
            'no' => $param['no'],
            'volume_no' => $param['volume_no'],
            'name' => $param['name'],
            'uname' => $param['uname'],
            'open' => $param['open'],
            'page_title' => $param['page_title'],
            'page_keyword' => $param['page_keyword'],
            'page_description' => $param['page_description'],
            'description' => $param['description'],
            'mob_description' => $param['mob_description'],
            'status' => $param['status'],
        	'prev_no' => $param['prev_no'],
        	'next_no' => $param['next_no']
        );
        $this->db->where('id', $param['id']);
        $this->db->update('chapter', $data);
    }

    public function updateChapterAjax($param){
        $data = array(
            'book_id' => $param['book_id'],
            'no' => $param['no'],
            'volume_no' => $param['volume_no'],
            'name' => $param['name'],
            'uname' => $param['uname'],
            'open' => $param['open'],
        	'prev_no' => $param['prev_no'],
        	'next_no' => $param['next_no']
        );
        $this->db->where('id', $param['id']);
        $this->db->update('chapter', $data);
    }

    public function deleteChapter($no, $uname){
        $this->db->where('no', $no);
        $this->db->where('uname', $uname);
        $this->db->delete('chapter');
    }

    public function getBookByUname($book_uname){
        $this->db->select(' book.id as book_id, book.name as book_name, book.uname as book_uname, book.image_id as book_image, book.author_id as book_author, page_title, page_keyword, 
                            page_description, description, mob_description, book.type as book_type, use_volume, status,
                            author.name as author_name, filename
                         ');
        $this->db->from('book');
        $this->db->join('author'        , 'book.author_id = author.id', 'inner');
        $this->db->join('image'         , 'book.image_id = image.id', 'left');
        $this->db->where("book.uname", $book_uname);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function updateChapterNo($param){
        $data = array(
            'no' => $param['no']
        );
        $this->db->where('id', $param['id']);
        $this->db->update('chapter', $data);
    }

    public function getChapterByBookID($book_id){
        $this->db->select(' id , no, volume_no, name, uname, book_id');
        $this->db->from('chapter');
        $this->db->where("book_id", $book_id);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function getChapterByBookIDByLimit($book_id, $number, $offset){
        $this->db->select(' chapter.id as chapter_id, chapter.no as chapter_no, chapter.volume_no as chapter_volume_no,
                            chapter.name as chapter_name, chapter.uname as chapter_uname, chapter.book_id as chapter_book_id,
                            chapter.open as chapter_open, next_no, prev_no,
                            book.name as book_name, volume.no as volume_no, volume.name as volume_name
                         ');
        $this->db->from('chapter');
        $this->db->join('book'        , 'book.id = chapter.book_id', 'inner');
        $this->db->join('volume'        , 'volume.no = chapter.volume_no AND volume.book_id = chapter.book_id', 'left');
        $this->db->where("chapter.book_id", $book_id);
        $this->db->order_by('chapter.id', 'desc'); 
        $this->db->limit($number, $offset);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getChapterByNo($no){
        $this->db->select(' chapter.id as chapter_id, chapter.no as chapter_no, chapter.volume_no as chapter_volume_no,
                            chapter.name as chapter_name, chapter.uname as chapter_uname, chapter.book_id as chapter_book_id,
                            chapter.open as chapter_open, next_no, prev_no,
                            book.name as book_name, volume.no as volume_no, volume.name as volume_name
                         ');
        $this->db->from('chapter');
        $this->db->join('book'        , 'book.id = chapter.book_id', 'inner');
        $this->db->join('volume'        , 'volume.no = chapter.volume_no AND volume.book_id = chapter.book_id', 'left');
        $this->db->where("chapter.no", $no);
        $this->db->order_by('chapter.id', 'desc');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function getChapterByBookIDByNo($bookID, $no){
        $this->db->select(' chapter.id as chapter_id, chapter.no as chapter_no, chapter.volume_no as chapter_volume_no,
                            chapter.name as chapter_name, chapter.uname as chapter_uname, chapter.book_id as chapter_book_id,
                            chapter.open as chapter_open, next_no, prev_no,
                            book.name as book_name, volume.no as volume_no, volume.name as volume_name
                         ');
        $this->db->from('chapter');
        $this->db->join('book'        , 'book.id = chapter.book_id', 'inner');
        $this->db->join('volume'        , 'volume.no = chapter.volume_no AND volume.book_id = chapter.book_id', 'left');
        $this->db->where("chapter.book_id", $bookID);
        $this->db->where("chapter.no", $no);
        $this->db->order_by('chapter.id', 'desc');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getChapterByBookIDByVolumeNo($book_id, $volume_no){
        $this->db->select(' id , no, volume_no, name, uname, book_id');
        $this->db->from('chapter');
        $this->db->where("book_id", $book_id);
        $this->db->where("volume_no", $volume_no);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getChapterByNoByUname($no, $uname){
        $this->db->select(' id , book_id, no, volume_no, name, uname, open, page_title, page_keyword, page_description, description, mob_description, status, prev_no, next_no');
        $this->db->from('chapter');
        $this->db->where("no", $no);
        $this->db->where("uname", $uname);
        $query = $this->db->get();
        return $query->row_array();
    }

    public function countChapter(){
        $this->db->select('count(chapter.id) as total');
        $this->db->from('chapter');       
        $query = $this->db->get();
        return $query->row('total');
    }

    public function countChapterByBookID($book_id){
        $this->db->select('count(chapter.id) as total');
        $this->db->from('chapter');  
        $this->db->where("chapter.book_id", $book_id);     
        $query = $this->db->get();
        return $query->row('total');
    }
    
    // Table Chapter Error
    
	public function getChapterError(){
        $this->db->select(' id, chapterID, book_name, chapter_no, date, url');
        $this->db->from('chapter_error');
        $query = $this->db->get();
        return $query->result_array();
	}
	
    public function deleteChapterError($chapterID){
        $this->db->where('id', $chapterID);
        $this->db->delete('chapter_error');
    }
}
?>